/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.paris5.master2.guitarmaster.bean.EcomProduct;
import com.paris5.master2.guitarmaster.bean.EcomProductType;
import com.paris5.master2.guitarmaster.bean.EcomUser;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomQueryException;
import com.paris5.master2.guitarmaster.exception.EcomValidationException;
import com.paris5.master2.guitarmaster.service.ServiceConvertorRemote;
import com.paris5.master2.guitarmaster.service.ServiceProductRemote;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Alice
 */
public class Rechercher extends EcomAction implements ServletRequestAware {

    private List<EcomProduct> searchProducts;
    private int countSearchProducts;
    private int trieValue;
    private int rechPrice;

    public int getCountSearchProducts() {
        return countSearchProducts;
    }

    public void setCountSearchProducts(int countSearchProducts) {
        this.countSearchProducts = countSearchProducts;
    }
    private String champRecherche;
    HttpServletRequest request;

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String getChampRecherche() {
        return champRecherche;
    }

    public void setChampRecherche(String champRecherche) {
        this.champRecherche = champRecherche;
    }

    public List<EcomProduct> getSearchProducts() {
        return searchProducts;
    }

    public void setSearchProducts(List<EcomProduct> searchProducts) {
        this.searchProducts = searchProducts;
    }

    public int getTrieValue() {
        return trieValue;
    }

    public void setTrieValue(int trieValue) {
        this.trieValue = trieValue;
    }

    public int getRechPrice() {
        return rechPrice;
    }

    public void setRechPrice(int rechPrice) {
        this.rechPrice = rechPrice;
    }

    @Override
    public String execute() {
        try {
            ServiceProductRemote serviceProductRemote = lookupServiceProductRemote();
            
            trieValue = (int) Double.parseDouble(request.getParameter("trieValue"));
            //System.out.println("rechPrice__" + rechPrice);
            if (rechPrice != -1) {
                if (rechPrice == 1) {
                    searchProducts = serviceProductRemote.getProductsPriceLessThan(Float.parseFloat("100"), 0, 30);
                    countSearchProducts = serviceProductRemote.countProductsPriceLessThan(Float.parseFloat("100"));
                } else if (rechPrice == 2) {
                    searchProducts = serviceProductRemote.getProductsPriceLessThan(Float.parseFloat("500"), 0, 30);
                    countSearchProducts = serviceProductRemote.countProductsPriceLessThan(Float.parseFloat("500"));
                } else if (rechPrice == 3) {
                    searchProducts = serviceProductRemote.getProductsPriceLessThan(Float.parseFloat("1000"), 0, 30);
                    countSearchProducts = serviceProductRemote.countProductsPriceLessThan(Float.parseFloat("1000"));
                } else if (rechPrice == 4) {
                    searchProducts = serviceProductRemote.getProductsPriceLessThan(Float.parseFloat("2000"), 0, 30);
                    countSearchProducts = serviceProductRemote.countProductsPriceLessThan(Float.parseFloat("2000"));
                }
 
            } else { 
                    if (trieValue == 1) {
                        searchProducts = (List<EcomProduct>) serviceProductRemote.searchProducts(champRecherche, 0, 30, trieValue, true);
                    } else {
                        searchProducts = (List<EcomProduct>) serviceProductRemote.searchProducts(champRecherche, 0, 30, trieValue, false);
                    }
                     countSearchProducts = serviceProductRemote.countSearchedProducts(champRecherche);
            }

           
            if (searchProducts == null) {
                setMessage("Aucun résultat trouvé");
            } else {
                setMessage(countSearchProducts + " résultat(s) trouvé(s)");
            }
            registerRate();
            return SUCCESS;
        } catch (EcomValidationException ex) {
            Logger.getLogger(Rechercher.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EcomQueryException ex) {
            Logger.getLogger(Rechercher.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ERROR;
    }
}
